<?php

namespace app\Admin\controller;

use think\Controller;
use think\Request;

class Cart extends Controller
{
    public function addCart(){
        $params = input();
        $cart = cookie('cart') ?: [];
        $goods_id =  $params['id'];
        $data = \app\Admin\model\Goods::find($goods_id)->toArray();
        $row = [
            'goods_name' => $data['goods_name'],
            'price'=>$data['price'],
            'number'=>1
        ];
        if (isset($cart[$goods_id])){
            $cart[$goods_id]['number'] += 1;
        }else{
            $cart[$goods_id]= $row;
        }
        cookie('cart',$cart,86400*7);
        echo  "<script>alert('添加购物车成功')</script>";
    }

    public function show(){
        $cart = cookie('cart');
        return view('index',['cart'=>$cart]);
    }
}
